package com.yanqu.road.dao.impl.activity.stronghold;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.CrossStrongholdEliminateDefendPatrons;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossStrongholdEliminatePatronsDaoImpl extends TempDataObjectDao<CrossStrongholdEliminateDefendPatrons> {
    public static String getStrongholdEliminateDefendPatronsSql = "select * from t_c_activity_stronghold_eliminate_patrons where activityId=? ";

    public Map<Integer,Map<String, List<CrossStrongholdEliminateDefendPatrons>>> getStrongholdEliminateDefendPatrons(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer,Map<String, List<CrossStrongholdEliminateDefendPatrons>>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getStrongholdEliminateDefendPatronsSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossStrongholdEliminateDefendPatrons crossStrongholdEliminateDefendPatrons = parse(rs);
                Map<String, List<CrossStrongholdEliminateDefendPatrons>> listMap = dataMap.get(crossStrongholdEliminateDefendPatrons.getRound());
                if (listMap == null) {
                    listMap = new ConcurrentHashMap<>();
                    dataMap.put(crossStrongholdEliminateDefendPatrons.getRound(), listMap);
                }
                List<CrossStrongholdEliminateDefendPatrons> defendPatronsList = listMap.get(crossStrongholdEliminateDefendPatrons.getUnionUid());
                if (defendPatronsList == null) {
                    defendPatronsList = new ArrayList<>();
                    listMap.put(crossStrongholdEliminateDefendPatrons.getUnionUid(), defendPatronsList);
                }
                defendPatronsList.add(crossStrongholdEliminateDefendPatrons);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public List<CrossStrongholdEliminateDefendPatrons> getEliminatePatronsList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossStrongholdEliminateDefendPatrons> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getStrongholdEliminateDefendPatronsSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossStrongholdEliminateDefendPatrons crossStrongholdEliminateDefendPatrons = parse(rs);
                dataList.add(crossStrongholdEliminateDefendPatrons);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String updateStrongholdEliminateDefendPatronsSql = "update t_c_activity_stronghold_eliminate_patrons set userBaseInfo=?,earnSpeed=?,curBlood=?,totalBlood=?,patrons=? where activityId=? and round=? and serverId=? and userId=? and unionUid=? and patronsIndex = ?";

    @Override
    protected boolean updateData(CrossStrongholdEliminateDefendPatrons crossStrongholdEliminateDefendPatrons) {
        crossStrongholdEliminateDefendPatrons.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossStrongholdEliminateDefendPatrons.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateDefendPatrons.getEarnSpeed().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateDefendPatrons.getCurBlood().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateDefendPatrons.getTotalBlood().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossStrongholdEliminateDefendPatrons.getPatrons())));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateDefendPatrons.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateDefendPatrons.getRound()));
        para.put(i++, new DbParameter(Types.BIGINT, crossStrongholdEliminateDefendPatrons.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossStrongholdEliminateDefendPatrons.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateDefendPatrons.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateDefendPatrons.getPatronsIndex()));
        if (getDb().executeNoneQuery(updateStrongholdEliminateDefendPatronsSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertStrongholdEliminateDefendPatronsSql = "insert into t_c_activity_stronghold_eliminate_patrons(activityId,round,serverId,userId,unionUid,patronsIndex,userBaseInfo,earnSpeed,curBlood,totalBlood,patrons) values (?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossStrongholdEliminateDefendPatrons crossStrongholdEliminateDefendPatrons) {
        crossStrongholdEliminateDefendPatrons.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateDefendPatrons.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateDefendPatrons.getRound()));
        para.put(i++, new DbParameter(Types.BIGINT, crossStrongholdEliminateDefendPatrons.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossStrongholdEliminateDefendPatrons.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateDefendPatrons.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateDefendPatrons.getPatronsIndex()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossStrongholdEliminateDefendPatrons.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateDefendPatrons.getEarnSpeed().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateDefendPatrons.getCurBlood().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateDefendPatrons.getTotalBlood().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossStrongholdEliminateDefendPatrons.getPatrons())));
        if (getDb().executeNoneQuery(insertStrongholdEliminateDefendPatronsSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossStrongholdEliminateDefendPatrons parseData(ResultSet rs) throws SQLException {
        CrossStrongholdEliminateDefendPatrons crossStrongholdEliminateDefendPatrons = new CrossStrongholdEliminateDefendPatrons();
        crossStrongholdEliminateDefendPatrons.setActivityId(rs.getInt("activityId"));
        crossStrongholdEliminateDefendPatrons.setRound(rs.getInt("round"));
        crossStrongholdEliminateDefendPatrons.setServerId(rs.getLong("serverId"));
        crossStrongholdEliminateDefendPatrons.setUserId(rs.getLong("userId"));
        crossStrongholdEliminateDefendPatrons.setPatronsIndex(rs.getInt("patronsIndex"));
        crossStrongholdEliminateDefendPatrons.setUnionUid(rs.getString("unionUid"));
        crossStrongholdEliminateDefendPatrons.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), UserBaseInfo.class));
        crossStrongholdEliminateDefendPatrons.setEarnSpeed(new BigInteger(rs.getString("earnSpeed")));
        crossStrongholdEliminateDefendPatrons.setCurBlood(new BigInteger(rs.getString("curBlood")));
        crossStrongholdEliminateDefendPatrons.setTotalBlood(new BigInteger(rs.getString("totalBlood")));
        crossStrongholdEliminateDefendPatrons.setPatrons(JSON.parseObject(rs.getString("patrons"), UserPatrons.class));
        return crossStrongholdEliminateDefendPatrons;
    }
}
